package sjtu.ist.warehouse4ws.jcr;



import org.junit.Test;
import org.springmodules.jcr.JcrCallback;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import javax.jcr.ImportUUIDBehavior;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;

import org.springmodules.jcr.JcrTemplate;




public class Testjcr  {
	private JcrTemplate template;

	public JcrTemplate getTemplate() {
		return template;
	}

	public void setTemplate(JcrTemplate template) {
		this.template = template;
	}
@Test
//	public Value testjcr(){
//		return (Value) template.execute(new JcrCallback() {
//			public Object doInJcr(Session session) throws RepositoryException {
//				 
//				Node root = session.getRootNode(); 
//
//				// 存储内容       rootNode为workspace的根节点。数据保存在Property中                <我们项目中对于模型文件的读取需要比较大的改动>
//				Node hello = root.addNode("parent"); 
//				Node world = hello.addNode("child"); 
//				world.setProperty("message", "Hello, World!"); 
//				session.save(); 
//
//				// 检索内容
//				Node node = root.getNode("parent/child"); 
//				System.out.println(node.getPath()); 
//				System.out.println(node.getProperty("message").getString()); 
//
//				// 删除内容
//				root.getNode("parent").remove(); 
//				session.save(); 
//				
//			//测试父节点删除，子节点自动删除	
////				Node node1 = root.getNode("parent/child");           
////				System.out.println(node1.getPath()); 
////				System.out.println(node1.getProperty("message").getString()); 
//
//				
//				return null;}
//		});
//		} 
	
public Value testjcrVersion(){
	return (Value) template.execute(new JcrCallback() {
	public Object doInJcr(Session session) throws RepositoryException {
	    try {

	        Node root = session.getRootNode();	        
	        
	            System.out.print("Importing xml... ");
	            // 创建一个非结构化节点，存储XML文件
	            Node node = root.addNode("importxml", "nt:unstructured");
	            node.setProperty("modelTitle", "业务对象模型");
	            node.setProperty("content", "我是业务对象模型");
	            node.setProperty("createTime", Calendar.getInstance());	            
	            FileInputStream xml = new FileInputStream("src/036ce0ef-ce1c-4dcf-940b-ebc1fdd87d1b.xml");
	            session.importXML("/importxml", xml, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
	            node.addMixin("mix:versionable");
	            xml.close();
	            session.save();
	            node.checkin();
	            
	            
	           
		        if (root.hasNode("importxml")) {
		        	  File outputFile = new File("outputxml.xml");
		        	  FileOutputStream out = new FileOutputStream(outputFile);
		        	  session.exportSystemView("/", out, false, false);
		        }
		        System.out.println("-----------------------------导出文件成功");
		  
		        node.checkout();
		        node.setProperty("content", "我是功能模型");
	            node.setProperty("createTime", Calendar.getInstance());	
	            FileInputStream xml1 = new FileInputStream("src/9bb9c8fa-2045-408e-bcde-cb9c898e25af.xml");
	            session.importXML("/importxml", xml1, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
		    	session.save();
		    	node.checkin();
	            
	        	Version version = node.checkin();
	        	VersionHistory vh = node.getVersionHistory();
	        	VersionIterator vi = vh.getAllVersions();
	        	vi.skip(1);
	        	while (vi.hasNext()) {
	        			 Version v = vi.nextVersion();
	        			 NodeIterator ni = v.getNodes();
	        			 while (ni.hasNext()) {
	        			  Node nv = ni.nextNode();
	        			  System.out.print(v.getName()+"版本   ");
	        			  System.out.print("文件内容：  "+nv.getProperty("content").getString()+"  ");
	        			  System.out.println("版本创建时间："+v.getCreated().getTime());
	        			 }
	        	}
	            session.logout();
	            System.out.println("done.");            
	        
	   
	    } catch (Exception e) {
	    	}		
				return null;}
		});
		} 
private  void dump(Node node) throws RepositoryException {
    // First output the node path
    System.out.println(node.getPath());
    // Skip the virtual (and large!) jcr:system subtree
    if (node.getName().equals("jcr:system")) {
        return;
    }

    // Then output the properties
    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
        Property property = properties.nextProperty();
        System.out.println(property.toString());
        if (property.getDefinition().isMultiple()) {
            // A multi-valued property, print all values
            Value[] values = property.getValues();
            for (int i = 0; i < values.length; i++) {
               // System.out.println(property.getPath() + " = " + values[i].getString());
            }
        } else {
            // A single-valued property
           // System.out.println(property.getPath() + " = " + property.getString());
        }
    }

    // Finally output all the child nodes recursively
    NodeIterator nodes = node.getNodes();
    while (nodes.hasNext()) {
        dump(nodes.nextNode());
    }
}
}

